<?php

namespace app\api\controller;

use think\Controller;
use think\Db;


class Main extends Controller
{
    /**
     * 登录检测
     * @param string $name
     * @param string $password 已MD5加密
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detectLogin($name = '', $password = '')
    {
        if ($name == '') {//用户名不存在
            return $this->returnArr('UnUser');
        }
        if ($password =='') {//密码不存在
            return $this->returnArr('UnPassword');
        }
        $user = Db::name('user')
            ->where(['username' => $name, 'flag' => 1])
            ->find();
        if (!checkArr($user)) {//用户名不存在
            return $this->returnArr('UnUser');
        }
        if ($password !== $user['password']) { //密码不正确
            return $this->returnArr('UnPassword');
        }
        if ($user['status'] == 2) {//账号已禁用
            return $this->returnArr('UnLock');
        }

//        if ($user['validity'] < time()) {//账号过期
//            return $this->returnArr('Overdue');
//        }
        return $this->returnArr($user, true);
    }

    /**
     * 获取基础字典数据
     * @param string $table
     * @param string $where
     * @param string $order
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDictionary($table = '', $where = '', $order = '')
    {
        if (empty($table)) {
            return [];
        }
        return Db::name($table)->where($where)->order($order)->select();
    }

    /**
     * 获取当前用户公司下的员工角色
     * @param string $company_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRole($company_id = '')
    {
        $where['company_id'] = $company_id;
        $where['flag'] = 1;
        $where['level'] = ['>', 2];
        $role = Db::name('auth_group')
            ->field('id,title')
            ->where($where)
            ->select();
        return $role;
    }

    /**
     * 获取指定角色下的用户列表
     * @param string $role_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserList($role_id = '')
    {
        $where['status'] = 1;
        $where['super'] = ['>', 2];
        $where['flag'] = 1;
        $where['role_id'] = $role_id;
        $user = Db::name('user')
            ->field('id,username')
            ->where($where)
            ->select();
        return $user;
    }

    /**
     * 获取指定用户下的支付账号
     * @param string $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAccount($user_id = '')
    {
        $account = Db::name('user_account')
            ->where('user_id', $user_id)
            ->select();
        if (checkArr($account) == false) {
            return [];
        }
        $account_ids = join(',', array_column($account, 'account_id'));
        $where['id'] = ['in', $account_ids];
        $where['status'] = 1;
        $accounts = Db::name('pay_account')
            ->where($where)
            ->select();
        return $accounts;
    }

    /**
     * 获取物流公司
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLogistics()
    {
        $where['type'] = 1;
        $where['pid'] = 0;
        $where['status'] = 1;
        $logistics = Db::name('logistics')
            ->field('id,logistics')
            ->where($where)
            ->select();
        return $logistics;
    }

    /**
     * 获取物流公司服务
     * @param string $pid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLogService($pid = '')
    {
        $where['type'] = 1;
        $where['pid'] = $pid;
        $where['status'] = 2;
        $logistics = Db::name('logistics')
            ->field('id,logistics')
            ->where($where)
            ->select();
        return $logistics;
    }

    /**
     * 获取当前公司下的所有用户
     * @param string $company_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllUser($company_id = '')
    {
        $where['company_id'] = $company_id;
        $where['status'] = 1;
        $where['flag'] = 1;
        $where['super'] = ['>', 2];
        $user = Db::name('user')
            ->field('id,username,role_id')
            ->where($where)
            ->select();
        return $user;
    }

    /**
     * 获取指定id下当前公司的角色列表
     * @param string $id
     * @param int $level
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRoleById($id = '', $level = 1)
    {
        $where['flag'] = 1;
        $where['id'] = ['in', $id];
        $where['level'] = ['>', $level];
        $role = Db::name('auth_group')
            ->field('id,title')
            ->where($where)
            ->select();
        return $role;
    }

    /**
     * 组装亚马逊产品url
     * @param $shop_id
     * @param string $asin
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function makeAmazonUrl($shop_id, $asin = '')
    {
        $store = DB::name('amazon_store')->where('id', $shop_id)->find();
        return $store['org'] . "/dp/" . $asin;
    }

    /**
     * 获取亚马逊有效店铺列表
     * @param string $company_id
     * @return array|false
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAmazonStore($company_id = '')
    {
        $where['company_id'] = $company_id;
        $where['status'] = 1;
        $res = DB::name('amazon_store')->where($where)->select();
        return arrayCombine($res, 'id');
    }

    /**
     * 获取违禁词
     * @param string $user_id
     * @param string $company_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function illegalWords($user_id = '', $company_id = '')
    {
        return Db::name('illegal_words')->field('words')->order('id asc')->select();
    }

    /**
     * 返回json
     * @param string $content
     * @param string $state
     */
    public function returnJson($state = 'Ok', $content = '')
    {
        echo json_encode(['State' => $state, 'content' => $content]);
        exit;
    }

    /**
     * 信息返回
     * @param string $msg
     * @param bool $state
     * @return array
     */
    public function returnArr($msg = '', $state = false)
    {
        return ['state' => $state, 'content' => $msg];
    }
}
